$(function() {
	$("#login-form").keydown(function(e) {
		var e = e || event,
			keycode = e.which || e.keyCode;
		if (keycode == 13) {
			$("#login").trigger("click");
		}
	});
	$("#login").click(function() {
		var name = $("#username").val().trim();
		var password = $("#password").val().trim();
		if (name == "") {
			alert("用户名不能为空");
			return;
		}
		if (password == "") {
			alert("密码不能为空");
			return;
		}
		login(name, password, function(data) {
			if (data.retCode == 0) {
				accessToken = data.result.accessToken;
				//商户cookie保留时间
				$.cookie(TOKEN, accessToken, {
					expires: getIDStorageTime()
				});

				location.href = "./cloudshop.html";
			} else {
				alert("发生错误，错误原因是：" + data.retInfo);
			}

		}, function(obj, msg, msg2) {
			alert("发生错误，错误信息是：" + msg);
		});
	});

});

function login(name, password, callback, error) {
	//	console.log(url + "/login" + "?name=" + name + "&password=" + password);
	$.ajax({
		type: 'POST',
		url: url + "/login",
		data: {
			"name": name,
			"password": password
		},
		dataType: "json",
		//		timeout: 3000,
		crossDomain: true,
		success: function(data) {
			callback(data);
			//			console.log("result:" + data.retCode + "," + data.retInfo);
		},
		beforeSend: function() {
			$(".loading").show();
		},
		error: function(obj, msg, msg2) {
			error(obj, msg, msg2);
		},
		complete: function() {
			$(".loading").hide();
		}
	});
}